package com.fuershiye.woman.monitor.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fuershiye.woman.monitor.beans.*;
import com.fuershiye.woman.monitor.constants.Massage;
import com.fuershiye.woman.monitor.model.Permission;
import com.fuershiye.woman.monitor.model.Role;
import com.fuershiye.woman.monitor.model.User;
import com.fuershiye.woman.monitor.service.AdminService;
import com.fuershiye.woman.monitor.service.PermissionService;
import com.fuershiye.woman.monitor.service.RoleService;
import com.fuershiye.woman.monitor.service.UserService;
import com.fuershiye.woman.monitor.shiro.credentials.PasswordHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author admin
 */
@Controller
@RequestMapping(value = "/manager/admin")
public class ManagerAdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;
    @Autowired
    UserService userService;

    //    @RequiresPermissions("manager:adminUserList")
    @RequestMapping(value = "/userList.do")
    @ResponseBody
    public Result userList(String name, Integer page, Integer pageSize) {
        List<ViewUser> result = adminService.listByCondition(name, 1, new Page(page, pageSize));
        int size = adminService.countByCondition(name, 1);
        return new Result(Massage.SUCCESS, new PageResult(result, size));
    }

    //    @RequiresPermissions("manager:adminUserAdd")
    @RequestMapping(value="/userAdd.do")
    @ResponseBody
    public boolean userAdd(String userName, String password, String tel, String email, int status, Long id) {

        User admin = null;
        if(id != null) {
            admin = adminService.get(id);
        }

        if(admin==null || StringUtils.isNotBlank(userName)){
            admin = adminService.findByName(userName);
        }

        if(admin==null){
            admin = new User();
            admin.setCreateTime(new Date(System.currentTimeMillis()));
            admin.setType(1);
        }

        admin.setUserName(userName);
        admin.setTel(tel);
        password = PasswordHelper.encryptPassword(userName,password);
        admin.setPassword(password);
        admin.setEmail(email);
        admin.setUpdateTime(new Date(System.currentTimeMillis()));
        admin.setStatus(status);
        adminService.save(admin);
        return true;
    }

    //    @RequiresPermissions("manager:adminUserTel")
    @RequestMapping(value = "/userTel.do")
    @ResponseBody
    public Result userTel() {
        List<ViewUserTel> list = userService.getSameAreaUserByNowUserRoleDepartment();
        return new Result(Massage.SUCCESS, new PageResult(list, list.size()));
    }
    //    @RequiresPermissions("manager:adminUserUpdate")
    @RequestMapping(value="/updateTel.do")
    @ResponseBody
    public boolean userAdd(Long  id, String tel, String name) {
        User admin = adminService.get(id);
        if(null==admin){
            System.out.println("user id null");
            return false;
        }
        admin.setTel(tel);
        admin.setName(name);
        admin.setType(2);
        admin.setUpdateTime(new Date(System.currentTimeMillis()));
        adminService.save(admin);
        return true;
    }

    //    @RequiresPermissions("manager:adminUserDelete")
    @RequestMapping(value="/userDelete.do")
    @ResponseBody
    public boolean userDelete(Long id) {
        User admin = adminService.get(id);
        long now = System.currentTimeMillis();
        Date nowDate = new Date(now);
        admin.setUpdateTime(nowDate);
        admin.setStatus(3);//修改为已删除
        adminService.save(admin);
        return true;
    }

    //    @RequiresPermissions("manager:adminRoleAll")
    @RequestMapping(value="/roleAll.do")
    @ResponseBody
    public String roleAll(Long id) {
        List<Long> roleIds = new ArrayList<Long>();
        User admin = adminService.get(id);
        List<Role> roles = admin.getRoles();
        for(int i=0;i<roles.size();i++) {
            roleIds.add(roles.get(i).getId());
        }
        JSONArray roleJson = new JSONArray();
        roleJson.add(roleIds);
        return roleJson.toString();
    }

    //    @RequiresPermissions("manager:adminRoleAdd")
    @RequestMapping(value="/roleAdd.do")
    @ResponseBody
    public String roleAdd(Long aid, Long rid) {
        User admin = adminService.get(aid);
        Role role = roleService.get(rid);
        List<Role> roles = admin.getRoles();
        roles.add(role);
        adminService.save(admin);
        JSONObject jsonObject = new JSONObject();
        return jsonObject.toJSONString();
    }

    //    @RequiresPermissions("manager:adminRoleDelete")
    @RequestMapping(value="/roleDelete.do")
    @ResponseBody
    public String roleDelete(Long aid,
                             Long rid) {
        User admin = adminService.get(aid);
        Role role = roleService.get(rid);
        List<Role> roles = admin.getRoles();
        roles.remove(role);
        adminService.save(admin);
        JSONObject jsonObject = new JSONObject();
        return jsonObject.toJSONString();
    }

    //    @RequiresPermissions("manager:adminPermissionList")
    @RequestMapping(value="/permissionList.do")
    public String permissionList(String page,Model model,HttpServletRequest request){
        List<Role> roleList = roleService.list();
        model.addAttribute("roleList",roleList);
        List<Permission> permissionList = permissionService.list();
        model.addAttribute("permissionList",permissionList);
        return "manager/" + page;
    }
    //    @RequiresPermissions("manager:adminPermissionList")
    @RequestMapping(value="/telView.do")
    public String telView(String page,Model model){
        return "manager/" + page;
    }

    //    @RequiresPermissions("manager:adminPermissionAll")
    @RequestMapping(value="/permissionAll.do")
    @ResponseBody
    public String permissionAll(Long id) {
        List<Long> pIds = new ArrayList<Long>();
        User admin = adminService.get(id);
        List<Permission> permissions = admin.getPermissions();
        for(int i=0;i<permissions.size();i++) {
            pIds.add(permissions.get(i).getId());
        }
        JSONArray roleJson = new JSONArray();
        roleJson.add(pIds);
        return roleJson.toString();
    }

    //    @RequiresPermissions("manager:adminPermissionAdd")
    @RequestMapping(value="/permissionAdd.do")
    @ResponseBody
    public String permissionAdd(Long aid, Long pid) {
        User admin = adminService.get(aid);
        Permission permission = permissionService.get(pid);
        List<Permission> permissions = admin.getPermissions();
        permissions.add(permission);
        adminService.save(admin);
        JSONObject jsonObject = new JSONObject();
        return jsonObject.toJSONString();
    }

    //    @RequiresPermissions("manager:adminPermissionDelete")
    @RequestMapping(value="/permissionDelete.do")
    @ResponseBody
    public String permissionDelete(Long aid, Long pid) {
        User admin = adminService.get(aid);
        Permission permission = permissionService.get(pid);
        List<Permission> permissions = admin.getPermissions();
        permissions.remove(permission);
        adminService.save(admin);
        JSONObject jsonObject = new JSONObject();
        return jsonObject.toJSONString();
    }
}
